/**
 * Created by Administrator on 2017/5/22 0022.
 */
/**
 * Created by Maibenben on 2017/5/19.
 */
$(function () {
    FastClick.attach(document.body);
    $('input[type!="file"]').off('focus').on('focus', function () {
        var _self=$(this);
        var ty=$(this).attr('type');
        $('.top_input').show();
        if(ty=='password'){
            $('.top_input input').attr('type',ty)
        }

        $('.top_input input').val(_self.val())
        $('.top_input input').focus();
        $('.top_input input').off('blur').on('blur', function () {
            var v=$('.top_input input').val();
            _self.val(v)
            $('.top_input').hide();
            $('.top_input input').attr('type','text')
        })
        $('.top_input input').off('keydown').on('keydown', function (e) {
            var key= e.which;
            if(key==13){
                var v=$('.top_input input').val();
                _self.val(v)
                $('.top_input').hide();
                $('.top_input input').attr('type','text')
            }
        })
    })

    //弹框函数
    function tip(msg) {
        $(".pop_tip").show().text(msg);
        setTimeout(function () {
            $(".pop_tip").slideUp(500);
        }, 1000)
    }

   // var status_bgm=0;

    function stopBgm(){
        if(status_bgm==0){
            status_bgm=1;
        }else{
            status_bgm=0;
        }
        cordovaPlugin.stopMusic(success, fail,status_bgm);
    }

    var success = function(message){
//             alert("success:\r\n "+message);
    };

    var fail = function(message){
//        alert("error:\r\n "+message);
    };

    //用户id
    var uid =getQueryString('userid');
    var ID=getQueryString('ID');
    var rewardlevel;
    var butler;
    var uname,wx;
    var stoolable;
    var certificationstate;
    //收集功能是否点击
    var shouji = false;
    //页面显示用户基本信息
    $('.shuaxin').off('touchstart').on('touchstart', function () {
        window.location.href='index.html?userid='+uid+'&ID='+ID
    })
    var pm = {};
    pm.userid = uid;
    getData({url: info, data: pm}, function (data) {
       setTimeout(function () {
           $('.loading').hide()
       },500)
        rewardlevel=data.rewardlevel;
        certificationstate=data.certificationstate;
        stoolable=data.rewardlevel;
        butler=data.butler;
        uname=data.name;
        wx=data.wxid;
        var Sign=data.sign;
        if(certificationstate>0){
            $('.shimingbtn').hide()
        }
        if(Sign==1){
            $('.sign').text('已签到')
        }else{
            $('.sign').text('签到')
        }
        var umobile = data.mobile;
        var uweixin = data.wxid;
        if(data.newnotifystate==1){
            $('.new_msg').show()
        }
        //管家时间
        var dt=data.butlersurplustime;
        if(dt!=''&& dt!= undefined){
            var time=dt.split(' ')[0];
            $('.time').html('剩余'+time)
        }
        if(data.wxid !=''){
            $('.change_info').hide()
        }
        if(data.bank !=''){
            $('.change_info').hide()
        }
//         农民等级
        if(data.rewardlevel==0){
            $(".user_lv").text('农民');
        }
        if(data.rewardlevel==1){
            $(".user_lv").text('中农');
        }
        if(data.rewardlevel==2){
            $(".user_lv").text('富民');
        }
        if(data.rewardlevel==3){
            $(".user_lv").text('地主');
        }
        //基本信息展示
        $('.lv span').html(data.level);
        $(".user_name").text(data.name);
        $(".user_id").text(data.ID);
        $(".user_mobile").text(data.mobile);
        $(".user_weixin").text(data.wxid);
        $(".user_alipay").text(data.alipayno);
        $('.user_bank_card').text(data.cardno);
        $('.user_bank_at').text(data.bank);
        $(".user_recommend").text(data.recommender);
        $(".recommend_mobile").text(data.recommendermobile);
        $(".integral>span").eq(0).text(parseInt(data.sesame));
        $(".integral>span").eq(1).children().text(parseInt(data.points));
        if(data.photo!=""){
            $(".face>img").attr("src", serverurl + data.photo);
        }
        $(".name").text(data.name);
        //实名认证
        switch (data.certificationstate) {
            case -1:
                $(".member_info_right strong").text("（认证未通过）");
                break;
            case 0:
                $(".member_info_right strong").text("（未认证）");
                break;
            case 1:
                $(".member_info_right strong").text("（认证中）");
                $(".shiming_btn").hide();
                break;
            case 2:
                $(".member_info_right strong").text("（已认证）");
                $(".shiming_btn").hide();
                break;
            default:
                $(".member_info_right strong").text("（认证未通过）");
                break;
        }
        $(".shiming_btn").on("click", function () {

        });
        //管家
//        if (data.butler == 0) {
//            $(".guanjia").hide();
//        } else {
//            $(".guanjia").show();
//        }
        //农民
        if (data.firstassistant == 1) {
            $(".one").show();
        }
        if (data.secassistant == 1) {
            $(".two").show();
        }
        if (data.thirdassistant == 1) {
            $(".three").show();
        }
        if (data.fourthassistant == 1) {
            $(".four").show();
        }
        //签到
        $(".sign").off('click').on("click", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            var signpm = {};
            signpm.userid = uid;
            getData({url: sign, data: signpm}, function (res) {
                if(res.msg=='success'){
                    tip(res.info);
                    $(".sign").text('已签到')
                }else{
                    tip(res.info)
                }

            })
        });
        //留言
        $(".words").off('click').on("click", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $("#box").show();
            $(".liuyan_box").show().siblings().hide();
            $(".liuyan_box>textarea").val("");
        });
        $(".btn").off('click').on("click", function () {
            var wordspm = {};
            wordspm.userid = uid;
            wordspm.content = $(".liuyan_box>textarea").val();
            getData({url: words, data: wordspm}, function (res) {
                if(res.msg=='success'){
                    tip(res.info);
                    $(".liuyan_box>textarea").val("");
                    $(".liuyan_box").hide()
                    $("#box").hide();
                }else{
                    tip(res.info)
                }

            })
        });
        //点击上传头像
        $(".inp").on("change", function () {
            var _this=$(this)
            var that = _this.parent();
//            var reader = new FileReader;
//            reader.readAsDataURL(this.files[0]);
            lrz(this.files[0],{quality:0.5})
                .then(function (re) {
                    that.prev().attr('src', re.base64);
                    $('.shangchuan').off('touchstart').on('touchstart', function () {
                        if(certificationstate!=2){
                            tip('请先实名认证');
                            return
                        }
                        var picpm = {};
                        picpm.userid = uid;
                        picpm.base64 =re.base64;
                        picpm.type = 1;
                        getData({url: headpic, data: picpm}, function (res) {
                            if (res.msg == "fail") {
                                tip(res.info);
                            }else{
                                tip(res.info);
                                var changedpm = {};
                                changedpm.userid = uid;
                                getData({url: info, data: changedpm}, function (data) {
                                    if(data.photo!=""){
                                        $(".face>img").attr("src", serverurl + data.photo);
                                        that.prev().attr('src','');
                                    }
                                })
                            }
                        });
                    })



                   // if (_this.files[0].type.indexOf("image") != -1) {

//                        reader.onload = function () {
//                            that.prev().css("background", "url(" + reader.result + ") no-repeat center/cover");
//
//                            var picpm = {};
//                            picpm.userid = uid;
//                            picpm.base64 = reader.result;
//                            picpm.type = 1;
//                            getData({url: headpic, data: picpm}, function (res) {
//                                if (res.msg == "fail") {
//                                    tip(res.info);
//                                }else{
//                                    tip(res.info)
//                                    var changedpm = {};
//                                    changedpm.userid = uid;
//                                    getData({url: info, data: changedpm}, function (data) {
//                                        if(data.photo!=""){
//                                            $(".face>img").attr("src", serverurl + data.photo);
//                                        }
//                                    })
//                                }
//                            });
//
//
//                        }
//                    } else {
//                        tip("请上传图片格式文件");
//                    }
                })
        });
        //点击头像打开个人信息
        $(".face").off('click').on("click", function () {
            $("#box").show();
            $(".member_info").show().siblings().hide();
            $(".member_info_left img").eq(0).attr('src',' ');
            $(".member_info_left input").val('')
        });
        //点击关闭弹框
        $(".close").off('click').on("click", function () {
            $("#box").hide();
            $(".kuojian_box").hide();
            $(".guoshi_list_box").hide();
            $(".woyaomai_box").hide();
            $(".cangku_box").hide();
            $(".guadan_box").hide();
            $(".search_box").hide();
            $(".haoyou_box").hide();
            $(".member_info").hide();
            $(".liuyan_box").hide();
            $(".search_result").text("");
            $(".search_mobile").val("");
            //重置
            $(".search_result_name").text("");
            $(".search_result_mobile").text("");
            $(".search_result_recommendname").text("");
            $(".search_result_recommendmobile").text("");
            $('.sell_guozi_box_content input').val('')
        });
        //实名认证

        //好友列表
        fr()
        function fr() {
            var pages;
            var pages2;
            var Num=0;
            var h;
            $(".user_friend").off('click').on("click", function () {
                if(certificationstate!=2){
                    tip('请先实名认证');
                    return
                }
                $("#box").show();
                $(".haoyou_box").show().siblings().hide();
                var friendpm = {};
                friendpm.userid = uid;
                friendpm.type = 1;
               // friendpm.page = 1;
                friendpm.rows = 10;
                friendpm.level=Num;
                getData({url: friendlist, data: friendpm}, function (res) {
                    $(".tuijian_box tbody").html(template("tuijianfriendTem", res));
                    pages=res.pages;
                    $('.num').text(res.total)
                    h=$(".haoyou_box_content").height()
                    lz()

                })
            });
            $(".haoyou_box_top>p").eq(0).off('click').on("click", function () {
                $(this).addClass("active").siblings().removeClass("active");
                $(".tuijianhaoyou").show().siblings(".qitahaoyou").hide();

                var friendpm = {};
                friendpm.userid = uid;
                friendpm.type = 1;
//                friendpm.page = 1;
                friendpm.rows = 10;
                friendpm.level=Num;
                getData({url: friendlist, data: friendpm}, function (res) {
                    $(".tuijian_box tbody").html(template("tuijianfriendTem", res));
                    $(".haoyoushuliang>span").text(res.total);
                    lz()
//                    $(".gof2").off('click').on("click", function () {
//                        var temphref=$(this).attr("href").split("&")[0]+"&userid="+uid;
//                        $(this).attr("href",temphref);
//                        if(stoolable==0){
//                            tip("您的直推好友不足五人，暂不可进入好友农场");
//                            return false;
//                        }
//                    });
                })
            });
            $('.tuijianhaoyou').off('touchstart').on('touchstart','.select_num', function () {
                $('.haoyou_box_content').css('pointer-events','none')
               // $(this).addClass('Now').siblings().removeClass('Now');
                $('.select_num').text('选择好友')
                $('.select_list').toggle();
                $('.select_list li').off('touchstart').on('touchstart', function () {
                    $('.select_num').text($(this).text());
                    $('.select_list').hide();
                    setTimeout(function () {
                        $('.haoyou_box_content').css('pointer-events','auto')
                    },500)
                    Num=$(this).index();
                    getData({url:friendlist,data:{userid:uid,type:1,rows:10,level:Num}}, function (data) {
                        $(".tuijian_box tbody").html(template("tuijianfriendTem", data));
                        pages=data.pages;
                        $('.num').text(data.total)
                           if(Num==0){
                               $('.hynum>span').eq(0).text('全部好友数量：')
                           }else if(Num==1){
                               $('.hynum>span').eq(0).text('一代好友数量：')
                           }else if(Num==2){
                               $('.hynum>span').eq(0).text('二代好友数量：')
                           }else if(Num==3){
                               $('.hynum>span').eq(0).text('三代好友数量：')
                           }
                        lz()
                    })
                })
                //var num=$(this).index();

               // for(var i=0;i<$('.tuijian_box tbody tr').length;i++){
                    //var Tr=$('.tuijian_box tbody tr').eq(i).find('td').eq(0).html();

//                    if(Tr.indexOf(num)>-1){
//                        $('.tuijian_box tbody tr').eq(i).css('display','')
//                    }else{
//                        $('.tuijian_box tbody tr').eq(i).css('display','none')
//                    }
               // }
            })
            $(".tuijian_box").off('click').on("click",'.gof2', function () {
                var frid=$(this).attr('fuserid');
                var sb=$(this).attr('sb');
                if(stoolable==0){
                    tip("您的直推好友不足五人，暂不可进入好友农场");
                }else{
                    if(rewardlevel==0){
                        tip('你的农民等级不够')
                    }
                    if(rewardlevel-sb>=0){
                        window.location.href='friend_index.html?stool=1&friendid='+frid+'&userid='+uid
                    }else{
                        tip('你的农民等级不够')
                    }
                    //window.location.href='friend_index.html?stool=1&friendid='+frid+'&userid='+uid
                }
            });
            function lz() {
                var page1=1;
                var loading1=false;
                $('.haoyou_box_content').off('scroll').on('scroll', function(){
                    var nScrollHight =  $(this)[0].scrollHeight; //滚动距离总长(注意不是滚动条的长度)
                    var nScrollTop = $(this)[0].scrollTop;  //滚动到的当前位置
                    var nDivHight = $(".haoyou_box_content").height();
                    if(nScrollTop + nDivHight >= nScrollHight){
                        console.log(pages)
                        if(loading1){
                            return;
                        }
                        loading1=true;
                        ++page1;
                        if(page1>pages){
                            tip('没有更多数据了')
                            return;
                        }
                        getData({url:friendlist,data:{userid:uid,page:page1,rows:10,type:1,level:Num}}, function (data) {
                            if(data.rows[0]==null){
                                //$('.haoyou_box_content').append('<p class="no-data">暂无数据</p>')
                            }else{
                                var html=template('tuijianfriendTem',data);
                                $('.tuijian_box tbody').append(html);
                                loading1=false;
                                pages=data.pages
                            }
                        });
                    }

                });
            }

           function lz2() {
               var page1=1;
               var loading2=false;
               $('.haoyou_box_content').off('scroll').on('scroll', function(){
                   var nScrollHight =  $(this)[0].scrollHeight; //滚动距离总长(注意不是滚动条的长度)
                   var nScrollTop = $(this)[0].scrollTop;  //滚动到的当前位置
                   var nDivHight =h;
                   if(nScrollTop + nDivHight >= nScrollHight){
                       console.log(pages2)
                       if(loading2){
                           return;
                       }
                       loading2=true;
                       ++page1;
                       if(page1>pages2){
                           tip('没有更多数据了')
                           return;
                       }
                       getData({url:friendlist,data:{userid:uid,page:page1,rows:10,type:2}}, function (data) {
                           if(data.rows[0]==null){
//                               $('.haoyou_box_content').append('<p class="no-data">暂无数据</p>')
                           }else{
                               var html=template('friendTem',data);
                               $('.otherfriend tbody').append(html);
                               loading2=false;
                               $(".gof").off('click').on("click", function () {
                                   var frid=$(this).attr('fuserid')
                                   window.location.href='friend_index.html?friendid='+frid+'&userid='+uid
                               });
                           }
                       });
                   }

               });
           }

            $(".haoyou_box_top>p").eq(1).off('click').on("click", function () {
                $(this).addClass("active").siblings().removeClass("active");
                $(".qitahaoyou").show().siblings(".tuijianhaoyou").hide();
                var friendpm = {};
                friendpm.userid = uid;
                friendpm.type = 2;
                friendpm.page=1;
                friendpm.rows = 10;
                getData({url: friendlist, data: friendpm}, function (res) {
                    pages2=res.pages;
                    $(".haoyoushuliang>span").text(res.total);
                    $(".otherfriend tbody").html(template("friendTem", res));
                    lz2();
                    $(".gof").off('click').on("click", function () {
                        var frid=$(this).attr('fuserid')
                        window.location.href='friend_index.html?friendid='+frid+'&userid='+uid
                    });
                })
            });
            //添加好友
            $(".tianjia").off('click').on("click", function () {
                $(".haoyou_box").hide();
                $(".addhy").show();
                //搜索指定好友
                $(".search2").off('click').on("click", function () {
                    var searchFriendpm = {};
                    searchFriendpm.type = 1;
                    searchFriendpm.context = $(".search_id").val();
                    getData({url: search, data: searchFriendpm}, function (res) {
                        if(res.msg=='success'){
                            $(".search_result_name").text(res.name);
                            $(".addhy_bot").off('click').on("click", function () {
                                var addfriendpm = {};
                                addfriendpm.userid = uid;
                                addfriendpm.invateduserid = res.userid;
                                getData({url: addfriend, data: addfriendpm}, function (res) {
                                    tip(res.info);
                                    if (res.msg == "success") {
                                        $(".addhy").hide();
                                        $("#box").hide();
                                    }
                                })
                            });
                        }else{
                            tip(res.info)
                        }

                    })
                });
            });
            //邀请好友
            $(".yaoqing").off('click').on("click", function () {
                $(".haoyou_box").hide();
                $(".yaoqinghaoyou").show();
                $(".yaoqinghaoyou_content table input").val('');
                //获取验证码
                $(".getcode").off('click').on("click", function () {
                    if (/^1[3|5|7|8][0-9]\d{4,8}$/.test($(".shouyao_mobile").val())) {
                        var getcodepm = {};
                        getcodepm.userid = uid;
                        getcodepm.type = 1;
                        getcodepm.mobile = $(".shouyao_mobile").val();
                        getData({url: getcode, data: getcodepm}, function (res) {
                            if(res.msg=='success'){
                                tip(res.info);
                            }else{
                                tip(res.info)
                            }

                        })
                    } else {
                        tip("请输入正确手机号");
                    }
                });
                $(".hy_bot").off('click').on("click", function () {
                    var invatepm = {};
                    invatepm.userid = uid;
                    invatepm.name = $(".shouyao_name").val();
                    invatepm.mobile = $(".shouyao_mobile").val();
                    invatepm.code = $(".shouyao_checkcode").val();
                    invatepm.loginpassword = $(".shouyao_loginword").val();
                    invatepm.paypassword = $(".shouyao_payword").val();
                    getData({url: invate, data: invatepm}, function (res) {
                        if(res.msg=='success'){
                            tip(res.info);
                            $('.yaoqinghaoyou').hide();
                            $('#box').hide();
                            $(".integral>span").eq(0).text(parseInt(res.sesame));
                        }else{
                            tip(res.info)
                        }

                    })
                });
            });
        }

        //搜索
        $(".user_search").on("click", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $("#box").show();
            $(".search_box").show();
        });
        $(".search1").off('click').on("click", function () {
            $(".search_result").text("");
            var searchpm = {};
            searchpm.type = 2;
            searchpm.context = $(".search_mobile").val();
            getData({url: search, data: searchpm}, function (res) {
                if (res.msg == "success") {
                    $(".search_result_name").text(res.name);
                    $(".search_result_mobile").text(res.mobile);
                    $(".search_result_recommendname").text(res.recommender);
                    $(".search_result_recommendmobile").text(res.recommendermobile);
                    //发送交易
                    $(".faqijiaoyi").off('click').on("click", function () {
                        var jiaoyipm = {};
                        jiaoyipm.userid = uid;
                        jiaoyipm.invateduserid = res.userid;
                        jiaoyipm.sesame = $(".order_count").val();
                        if ($(".search_result_name").text()) {
                            getData({url: jiaoyi, data: jiaoyipm}, function (res) {
                                if (res.msg == "success") {
                                    tip(res.info);
                                    $(".search_box").hide()
                                    $("#box").hide();
                                    $(".search_result_name").text("");
                                    $(".search_result_mobile").text("");
                                    $(".search_result_recommendname").text("");
                                    $(".search_result_recommendmobile").text("");
                                    $(".order_count").val("");
                                    $(".search_mobile").val("");
                                } else {
                                    tip(res.info);
                                }
                            })
                        } else {
                            tip("没有交易对象");
                        }
                    });
                } else {
                    tip(res.info);
                }
            })
        });
        //仓库
        $(".cangku_box_top>p").off('click').on("click", function () {
            $(this).addClass("cur").siblings().removeClass("cur");
        });
        $(".user_store").off('click').on("click", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $("#box").show();
            $(".cangku_box").show().siblings().hide();
            $(".cangku_box_content>table").hide();
            $("#tongji_chart").hide();
            $(".guozi_list").show();
            $(".cangku_box_top>p").eq(0).addClass("cur").siblings().removeClass("cur");
            var storepm = {};
            storepm.userid = uid;
            getData({url: store, data: storepm}, function (res) {
                $(".guozi_list").html(template("storeguozi", res));
                //售卖果子
                $(".sell").off('click').on("click", function () {
                    var guoziID= $(this).attr('guoziID');
                    $("#box").show();
                    $(".sell_guozi_box").show();
                    $(".sell_guozi_box_bot_p1").off('click').on("click", function () {
                        var sellpm = {};
                        sellpm.userid = uid;
                        sellpm.userwarehouseid = guoziID;
                        var pwds = MD5($(".sell_guozi_box_content>input").val() + 'runfEngkj2017sesAmeParadIse168');
                        sellpm.paypassword = pwds;
                        getData({url: sell, data: sellpm}, function (res) {
                            if(res.msg=="success"){
                                tip(res.info);
                                $(".sell_guozi_box").hide();
                                $('.cangku_box').hide();
                                $('#box').hide();
                                $(".sell_guozi_box_content>input").val("");
                                $('.integral span').eq(0).text(parseInt(res.sesame));
                                $(".integral>span").eq(1).children().text(parseInt(res.points));
                                $('.lv span').text(parseInt(res.level))
                            }else{
                                tip(res.info);

                            }
                        })
                    });
                    $(".sell_guozi_box_bot_p2").off('click').on("click", function () {
                        $(".sell_guozi_box").hide();
                        $(".sell_guozi_box_content>input").val("");
                    });
                });
            })
        });
       // 果子
        $(".guozi").off('click').on("click", function () {
            $("#box").show();
            $(".cangku_box").show().siblings().hide();
            $(".cangku_box_content>table").hide();
            $("#tongji_chart").hide();
            $(".guozi_list").show();
            $(this).addClass('cur').siblings().removeClass('cur')
            var storepm = {};
            storepm.userid = uid;
            getData({url: store, data: storepm}, function (res) {
                $(".guozi_list").html(template("storeguozi", res));
                //售卖果子
                $(".sell").off('click').on("click", function () {
                    $("#box").show();
                    $(".sell_guozi_box").show();
                    $(".sell_guozi_box_bot_p1").off('click').on("click", function () {
                        var sellpm = {};
                        sellpm.userid = uid;
                        sellpm.userwarehouseid = 10;
                        var pwds = MD5($(".sell_guozi_box_content>input").val() + 'runfEngkj2017sesAmeParadIse168');
                        sellpm.paypassword = pwds;
                        getData({url: sell, data: sellpm}, function (res) {
                                if(res.msg=="success"){
                                    tip(res.info);
                                    $(".sell_guozi_box").hide();
                                    $(".sell_guozi_box_content>input").val("");
                                }else{
                                    tip(res.msg);

                                }
                        })
                    });
                    $(".sell_guozi_box_bot_p2").off('click').on("click", function () {
                        $(".sell_guozi_box").hide();
                        $(".sell_guozi_box_content>input").val("");
                    });
                });
            })
        });
        //收获
        $(".shouhuo").off('click').on("click", function () {
            $(".guozi_list").hide();
            $(".cangku_box_content>table").hide();
            $("#tongji_chart").hide();
            $(this).addClass('cur').siblings().removeClass('cur')
            var shouhuopm = {};
            shouhuopm.userid = uid;
            shouhuopm.type = 1;
            getData({url: count, data: shouhuopm}, function (res) {
                $(".cangku_box_content>table").show();
                $(".cangku_box_content>table>tbody").html(template("shouhuojilu", res));
            })
        });
        //收取
        $(".shouqu").off('click').on("click", function () {
            $(".guozi_list").hide();
            $(".cangku_box_content>table").hide();
            $("#tongji_chart").hide();
            $(this).addClass('cur').siblings().removeClass('cur')
            var shouqupm = {};
            shouqupm.userid = uid;
            shouqupm.type = 2;
            getData({url: count, data: shouqupm}, function (res) {
                $(".cangku_box_content>table").show();
                $(".cangku_box_content>table>tbody").html(template("shouhuojilu", res));
            })
        });
        //偷取
        $(".touqu").off('click').on("click", function () {
            $(".guozi_list").hide();
            $(".cangku_box_content>table").hide();
            $("#tongji_chart").hide();
            $(this).addClass('cur').siblings().removeClass('cur')
            var touqupm = {};
            touqupm.userid = uid;
            touqupm.type = 3;
            getData({url: count, data: touqupm}, function (res) {
                $(".cangku_box_content>table").show();
                $(".cangku_box_content>table>tbody").html(template("shouhuojilu", res));
            })
        });
        //统计
        $(".tongji").off('click').on("click", function () {
            $(".guozi_list").hide();
            $(".cangku_box_content>table").hide();
            $(this).addClass('cur').siblings().removeClass('cur')
            var tongjipm = {};
            tongjipm.userid = uid;
            tongjipm.year = (new Date()).getFullYear();
            getData({url: tongji, data: tongjipm}, function (res) {
                $("#tongji_chart").show();
                var tongjidata = [];
                res.incomestatic.forEach(function (v) {
                    tongjidata.push(v.income);
                });
                var myChart = echarts.init(document.getElementById('tongji_chart'));
                option = {
                    title: {
                        text: '2017'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            dataZoom: {
                                yAxisIndex: 'none'
                            },
                            dataView: {readOnly: false},
                            magicType: {type: ['line', 'bar']},
                            restore: {},
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
                    },
                    yAxis: {},
                    series: [
                        {
                            name: '收益统计',
                            type: 'line',
                            data: tongjidata
                        }
                    ],
                    color: ["rgb(96, 14, 2)"]
                };
                myChart.setOption(option);
            })
        });
        //挂单
        $(".user_guadan").on("touchstart", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $(".guadan_box").show().siblings().hide();
            $("#box").show();
        });
        $(".small_box_close").off('touchstart').on("touchstart", function () {
            $(".guadan_box").hide();
            $(".sell_guozi_box").hide();
            $(".sellStep1_box").hide();
            $(".sell_guozi_box_content>input").val("");
            $(".sellStep2_box").hide();
            $("#box").hide();
        });
        //我要买
        $(".guadan_box_content>p").eq(0).on("click", function () {
            $(".guadan_box").hide();
            $(".woyaomai_box").show();
            var guadanlistpm = {};
            guadanlistpm.userid = uid;
            guadanlistpm.type = 1;
            guadanlistpm.page = 1;
            guadanlistpm.rows = 20;
            getData({url: guadanlist, data: guadanlistpm}, function (res) {

                console.log(template("guadanTem", res));
                $(".woyaomai_box_content tbody").html(template("guadanTem", res));
                $('.mai_quxiao').off('touchstart').on('touchstart', function () {
                        var orderId=$(this).attr('orderid');
                    getData({url:cancelorder,data:{userid:uid,orderid:orderId}}, function (data) {
                        if(data.msg=='success'){
                            tip(data.info);
                        }else{
                            tip(data.info)
                        }
                    })
                })
//                $(".detail_caozuo").on("click", function () {
//                    if ($(this).attr("data-state") > 0) {
//                        console.log(template("guadandetailTem", res));
//                        $(".woyaomai_div").html(template("guadandetailTem", res));
//                        $(".woyaomai_box_content_detail").show();
//                        //点击上传截图
//                        $(".woyaomai_up>input").on("change", function () {
//                            var that = $(this).parent();
//                            var reader = new FileReader;
//                            reader.readAsDataURL(this.files[0]);
//                            if (this.files[0].type.indexOf("image") != -1) {
//                                reader.onload = function () {
//                                    // that.prev().css("background", "url(" + reader.result + ") no-repeat center/cover");
//                                    //上传到接口
//                                    var jietupm = {};
//                                    jietupm.userid = uid;
//                                    jietupm.base64 = reader.result;
//                                    jietupm.type = 2;
//                                    getData({url: headpic, data: jietupm}, function (res) {
//                                        if (res.msg == "fail") {
//                                            tip(res.info);
//                                        } else if (res.msg == "success") {
//                                            that.prev().attr("src", serverurl + res.url);
//                                            //确认订单
//                                            $(".woyaomai_bot>img").eq(1).on("click", function () {
//                                                var comfirmbuypm = {};
//                                                comfirmbuypm.userid = uid;
//                                                comfirmbuypm.notifyid = uid;
//                                                comfirmbuypm.state = 2;
//                                                comfirmbuypm.thumbnail = res.url;
//                                                getData({url: comfirmbuy, data: comfirmbuypm}, function (data) {
//                                                    console.log(data);
//                                                    if (data.msg == "success") {
//                                                        tip(data.info);
//                                                        $(".woyaomai_box_content_detail").hide();
//                                                        $("#box").hide();
//                                                    } else {
//                                                        tip(data.info);
//                                                    }
//                                                })
//                                            });
//                                        }
//                                    });
//                                }
//                            } else {
//                                tip("请上传图片格式文件");
//                            }
//                        });
//                    }
//                    //确认交易
//                    $(".woyaomai_bot>img").eq(1).off('click').on("click", function () {
//                        if (!$(".woyaomai_pic").attr("src")) {
//                            tip("请上传转账截图");
//                        }
//                    });
//                    //取消交易
//                    $(".woyaomai_bot>img").eq(0).off('click').on("click", function () {
//                        $(".woyaomai_box_content_detail").hide();
//                        var cancelbuypm = {};
//                        cancelbuypm.userid = uid;
//                        cancelbuypm.notifyid = uid;
//                        cancelbuypm.state = -1;
//                        getData({url: comfirmbuy, data: cancelbuypm}, function (data) {
//                            console.log(data);
//                            tip(data.info);
//                        })
//                    });
//                });
            })
        });
        $(".small_box_close_buy").on("click", function () {
            $(".woyaomai_box_content_detail").hide();
        });
        $(".close").on("click", function () {
            $(".woyaomai_box_content_detail").hide();
        });
        function mm(a) {
            $(".sellStep1_box").show();
            $('.sellStep1_box p').off('touchstart').on('touchstart', function () {
                var mai_num=$(this).attr('data-count');
                $('.sellStep1_box').hide()
                $('.sellStep2_box').show();
                $('.sellStep2_box li').eq(0).find('span').eq(1).text(uname);
                $('.sellStep2_box li').eq(1).find('span').eq(1).text(mai_num);
                $('.sellStep2_box li').eq(2).find('span').eq(1).text(wx);
                $('.sellStep2_box .sell_cancel').off('touchstart').on('touchstart', function () {
                    $('.sellStep2_box').hide();
                    $('#box').hide()
                })
                $('.sellStep2_box .sell_sure').off('touchstart').on('touchstart', function () {
                    var val=$('.sellStep2_box li').eq(3).find('input').val()
                    var zfpwd=MD5(val+token);
                    console.log(zfpwd)
                    getData({url:guadan,data:{userid:uid,type:a,sesame:mai_num,paypassword:zfpwd}}, function (data) {
                        if(data.msg=='success'){
                            tip(data.info);
                            $('.sellStep2_box li').eq(3).find('input').val('');
                            $('.sellStep2_box').hide();
                            $('#box').hide()
                        }else{
                            tip(data.info)
                        }
                    })
                })
            })
        }
        //我要买发起挂单
        $(".faqiguadan").off('click').on("click", function () {
            $(".woyaomai_box").hide();
            mm(1)
        });
        //我要卖
        $(".guadan_box_content p").eq(1).on('touchstart' ,function () {
            $(".guadan_box").hide();
            $('.woyaosell_box').show();
            getData({url:guadanlist,data:{userid:uid,type:2}}, function (data) {
                    var html=template('guandanSell',data);
                    $('.woyaosell_box_content table tbody').html(html);
                    //点击取消
                    $('.sell_quxiao').off('touchstart').on('touchstart', function () {
                        var _self=$(this)
                        var orderid=$(this).attr('orderid');
                            getData({url:cancelorder,data:{userid:uid,orderid:orderid}}, function (data) {
                                 if(data.msg=='success'){
                                     tip(data.info);
                                 }else{
                                     tip(data.info)
                                 }
                            })
                    })
            })
            $('.sell_faqi').off('touchstart').on('touchstart', function () {
                $('.woyaosell_box').hide()
                mm(2)
            })
        })

        //种植
        $(".tudi>div").off('click').on("click", function () {
            if($(this).css("background").indexOf("lvtudi")!=-1){
                return false;
            }
            if($(this).attr("data-ex")==1){
                tip("已经种上植物或者失效的土地不能使用");
                return false;
            }
            $("#box").show();
            var tudino = $(this).index() + 1;
            $(".guoshi_list_box").show();
                getData({url:fruitlist,data:{}}, function (data) {
                      var html=template('list',data);
                       $('.guoshi_list_box ul').html(html);
                    $(".guoshi_list_box li").on("click", function () {
                        $("#box").hide();
                        $(".guoshi_list_box").hide();
                        var frID=$(this).attr('fruitid')
                        var zhongzhipm = {};
                        zhongzhipm.userid = uid;
                        zhongzhipm.landno = tudino;
                        zhongzhipm.fruitid = frID;
                        getData({url: zhongzhi, data: zhongzhipm}, function (res) {
                            if (res.msg == "success") {
                                $(".integral>span").eq(0).text(parseInt(res.sesame));
                                tip(res.info);
                                //重新显示最新的数据
                                //植物信息列表
                                var zhiwulistpm = {};
                                zhiwulistpm.userid = uid;
                                getData({url: zhiwulist, data: zhiwulistpm}, function (res) {
                                    //植物显示位置
                                    $(".zhiwupic_container").html(template("landTem", res));
                                    $(".fpic0").css({width: "65px", position: "absolute", left: "27%", top: "32%"});
                                    $(".fpic1").css({width: "65px", position: "absolute", left: "18%", top: "40%"});
                                    $(".fpic2").css({width: "65px", position: "absolute", left: "35%", top: "40%"});
                                    $(".fpic3").css({width: "65px", position: "absolute", left: "26%", top: "49%"});
                                    $(".fpic4").css({width: "65px", position: "absolute", left: "44%", top: "48%"});
                                    $(".fpic5").css({width: "65px", position: "absolute", left: "35%", top: "56%"});
                                    $(".fpic6").css({width: "65px", position: "absolute", left: "52%", top: "54%"});
                                    $(".fpic7").css({width: "65px", position: "absolute", left: "43%", top: "64%"});
                                    $(".fpic8").css({width: "65px", position: "absolute", left: "61%", top: "63%"});
                                    $(".fpic9").css({width: "65px", position: "absolute", left: "52%", top: "72%"});
                                    //点击植物查看信息或者收取
                                    $(".fpic").on("click", function (e) {
                                        if (!shouji) {
                                            var myzhiwupm = {};
                                            myzhiwupm.userid = uid;
                                            myzhiwupm.userfruitid = $(this).attr("data-userfruitid");
                                            getData({url: myzhiwu, data: myzhiwupm}, function (res) {
                                                $(".zhiwu_detail_tip").show().css({left: e.clientX - 60, top: e.clientY - 77});
                                                $(".zhiwu_zhonglei").text("种类：" + res.fruit);
                                                $(".zhiwu_rate").text("生长率：" + res.growthrate);
                                                $(".zhiwu_count").text("拥有数量：" + res.totalincome.toFixed(2));
                                                $(".zhiwu_next").text("下一植物：" + res.nextlevel);
                                                $(window).on("click", function () {
                                                    $(".zhiwu_detail_tip").hide();
                                                });
                                            })
                                        } else {
                                            var collectpm = {};
                                            collectpm.userid = uid;
                                            collectpm.userfruitid = $(this).attr("data-userfruitid");
                                            getData({url: collect, data: collectpm}, function (res) {
                                                tip(res.info);
                                            })
                                        }
                                    });
                                })
                            } else {
                                tip(res.info);
                            }
                        })
                    });
                })

        });
    });
    //植物信息列表
    var zhiwulistpm = {};
    zhiwulistpm.userid = uid;
    getData({url: zhiwulist, data: zhiwulistpm}, function (res) {
       for(var i=0;i<res.rows.length;i++){
           $(".tudi>div").get(i).setAttribute("data-ex",res.rows[i].existfruit);
       }
        //红土地
        for (var i = 0; i < res.total; i++) {
            $(".tudi>div").eq(i).css("backgroundImage", 'url("imgs/hongtudi.png")').attr("data-ex",res.existfruit);
        }
        //扩建
        var kuojianaddress = [
            {top: "33%", left: "27%"},
            {top: "42%", left: "18%"},
            {top: "41%", left: "36%"},
            {top: "50%", left: "27%"},
            {top: "49%", left: "44%"},
            {top: "58%", left: "35%"},
            {top: "56%", left: "53%"},
            {top: "66%", left: "44%"},
            {top: "64%", left: "62%"},
            {top: "73%", left: "53%"},
            {top: "-10000px", left: "-10000px"}
        ];//扩建图标位置
        $("#kj_pic").css(kuojianaddress[res.total]);
        $("#kj_pic").on("click", function () {
            $("#box").show();
            $(".kuojian_box").show();
        });
        $(".kj_cancel").on("click", function () {
            $("#box").hide();
            $(".kuojian_box").hide();
        });
        $(".kj_determine").on("click", function () {
            var jiancepm = {};
            jiancepm.userid = uid;
            getData({url: zhiwulist, data: jiancepm}, function (res) {
                var kuojianpm = {};
                kuojianpm.userid = uid;
                kuojianpm.landno = res.total + 1;
                $("#box").hide();
                $(".kuojian_box").hide();
                getData({url: kuojian, data: kuojianpm}, function (res) {
                    $("#box").hide();
                    $(".kuojian_box").hide();
                    if (res.msg == "success") {
                        var zhiwulistpm = {};
                        zhiwulistpm.userid = uid;
                        getData({url: zhiwulist, data: zhiwulistpm}, function (res) {
                            kuojianpm.landno = res.total + 1;
                            $("#kj_pic").css(kuojianaddress[res.total]);
                            //红土地
                            for (var i = 0; i < res.total; i++) {
                                $(".tudi>div").eq(i).css("backgroundImage", 'url("imgs/hongtudi.png")');
                            }
                        });
                    }
                    tip(res.info);
                })
            });
        });
        //植物显示位置
        $(".zhiwupic_container").html(template("landTem", res));
        $(".fpic0").css({width: "65px", position: "absolute", left: "27%", top: "32%"});
        $(".fpic1").css({width: "65px", position: "absolute", left: "18%", top: "40%"});
        $(".fpic2").css({width: "65px", position: "absolute", left: "35%", top: "40%"});
        $(".fpic3").css({width: "65px", position: "absolute", left: "26%", top: "49%"});
        $(".fpic4").css({width: "65px", position: "absolute", left: "44%", top: "48%"});
        $(".fpic5").css({width: "65px", position: "absolute", left: "35%", top: "56%"});
        $(".fpic6").css({width: "65px", position: "absolute", left: "52%", top: "54%"});
        $(".fpic7").css({width: "65px", position: "absolute", left: "43%", top: "64%"});
        $(".fpic8").css({width: "65px", position: "absolute", left: "61%", top: "63%"});
        $(".fpic9").css({width: "65px", position: "absolute", left: "52%", top: "72%"});
        //点击植物查看信息或者收取
        $(".fpic").on("click", function (e) {
            if (!shouji) {
                var myzhiwupm = {};
                myzhiwupm.userid = uid;
                myzhiwupm.userfruitid = $(this).attr("data-userfruitid");
                getData({url: myzhiwu, data: myzhiwupm}, function (res) {
                    $(".zhiwu_detail_tip").show().css({left: e.clientX - 60, top: e.clientY - 77});
                    $(".zhiwu_zhonglei").text("种类：" + res.fruit);
                    $(".zhiwu_rate").text("生长率：" + res.growthrate);
                    $(".zhiwu_count").text("拥有数量：" + res.totalincome.toFixed(2));
                    $(".zhiwu_next").text("下一植物：" + res.nextlevel);
                    $(window).on("click", function () {
                        $(".zhiwu_detail_tip").hide();
                    });
                })
            } else {
                var collectpm = {};
                collectpm.userid = uid;
                collectpm.userfruitid = $(this).attr("data-userfruitid");
                getData({url: collect, data: collectpm}, function (res) {
                    tip(res.info);
                })
            }
        });
        //收集植物
        var bool = [false, true];
        var boolcount = 0;
        $(".shouji").off('click').on("click", function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            boolcount++;
            shouji = bool[boolcount % 2];
            $(this).toggleClass("animation");
        });
    });

    //消息
    xiaoxi()
    function xiaoxi() {
        var pages=0;
        var xiaoxiID=0;
        $('.news').on('touchstart', function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $('.new_msg').hide()
            $('#box').show();
            $('.xiaoxi_box').siblings().hide();
            $('.xiaoxi_box').show();
            $('.xiaoxi_box_content').html('');

            //请求数据
            getData({url:newsurl,data:{userid:uid,page:1,rows:10}}, function (data) {
                if(data.rows==""){
                    $('.xiaoxi_box_content').append('<p class="no-data">暂无数据</p>')
                }else{
                    var html=template('news',data);
                    $('.xiaoxi_box_content').html(html);
                    pages=data.pages;
                }
            });
        });
        var page=1;
        var loading=false;
        //下拉加载数据
        $('.xiaoxi_box_content').on('scroll', function(){
            var nScrollHight =  $(this)[0].scrollHeight; //滚动距离总长(注意不是滚动条的长度)
            var nScrollTop = $(this)[0].scrollTop;  //滚动到的当前位置
            var nDivHight = $(".xiaoxi_box_content").height();
            if(nScrollTop + nDivHight >= nScrollHight){
                if(loading){
                    return;
                }
                loading=true;
                ++page;
                if(page>pages){
                    tip('没有更多数据了')
                    return;
                }
                getData({url:newsurl,data:{userid:uid,page:page,rows:10}}, function (data) {
                    if(data.rows[0]==null){
                        $('.xiaoxi_box_content').append('<p class="no-data">暂无数据</p>')
                    }else{
                        var html=template('news',data);
                        $('.xiaoxi_box_content').append(html);
                        loading=false;
                    }
                });
            }

        });
        //更新消息列表
        function again() {
            getData({url:newsurl,data:{userid:uid,page:1,rows:10}}, function (data) {
                if(data.rows[0]==null){
                    $('.xiaoxi_box_content').append('<p class="no-data">暂无数据</p>')
                }else{
                    var html=template('news',data);
                    $('.xiaoxi_box_content').html(html);
                }
                $('.xiaoxi_box_content').on('scroll', function(){
                    var nScrollHight =  $(this)[0].scrollHeight; //滚动距离总长(注意不是滚动条的长度)
                    var nScrollTop = $(this)[0].scrollTop;  //滚动到的当前位置
                    var nDivHight = $(".xiaoxi_box_content").height();
                    if(nScrollTop + nDivHight >= nScrollHight){
                        if(loading){
                            return;
                        }
                        loading=true;
                        ++page;
                        if(page>pages){
                            tip('没有更多数据了')
                            return;
                        }
                        getData({url:newsurl,data:{userid:uid,page:page,rows:10}}, function (data) {
                            if(data.rows[0]==null){
                                $('.xiaoxi_box_content').append('<p class="no-data">暂无数据</p>')
                            }else{
                                var html=template('news',data);
                                $('.xiaoxi_box_content').append(html);
                                loading=false;
                            }
                        });
                    }

                });
            });
        }
//        查看消息详情
        stopTouchendPropagationAfterScroll();
        $('.xiaoxi_box_content').on('touchend','.list' ,function () {
                $('.xiaoxi_mima').hide()
                $('.bot').removeAttr('style');
                $('.div').removeAttr('style');
                $('.btn1').removeAttr('style');
                $('.btn1').html('<img src="imgs/1_03.png" alt=""/>')
                $('.btn2').removeAttr('style');
                $('.pic').show();
                $('.pic').html('');
                $('.pic').append('<img class="xiaoxi_pic" src="" alt=""/>');
                $('.pic').append('<a href="javascript:;" target="_blank" class="up"><input class="jietu" type="file" accept="image/*"/></a>');
                $('.all').show();
                $('.xiaoxi_detail').show();
                var _self=$(this);
                var msgtype=_self.attr('msgtype')
                var state=_self.attr('state');
                var notifyid=_self.attr('news_id');
                xiaoxiID=notifyid
                var type=_self.attr('news_type');
                var title=_self.attr('content');//
                var name=_self.attr('name'); //姓名
                var wx=_self.attr('wxid');//微信
                var tel=_self.attr('mobile');//手机号
                var zhifubao=_self.attr('aliplayno');//支付宝
                var bank=_self.attr('bank');//银行
                var card=_self.attr('cardno');//卡号
                var orderno=_self.attr('orderno');//单号
                var pic=_self.attr('thumbnail');
                console.log(pic);
                var pic_bese64;
                $('.news_detail_title').html(title)
                $('.news_name').html(name);
                $('.news_wx').html(wx);
                $('.news_tel').html(tel);
                $('.news_zhifubao').html(zhifubao);
                $('.news_bank').html(bank);
                $('news_bankcard').html(card);
                $('.new_orderno').html(orderno);
                $('.xiaoxi_pic').attr('src',serverurl+pic);
                $('.img img').attr('src',$('.xiaoxi_pic').attr('src'));
                $('.jietu').val('');
               //上传图片前压缩
                $('.jietu').on('change', function () {
                        lrz(this.files[0],{quality:0.5})
                            .then(function (re) {
                                 console.log(re.base64);
                                $('.xiaoxi_pic').attr("src",re.base64);
                                $('.img img').attr('src',re.base64);
                                getData({url: uppicurl, data: {userid:uid,base64:re.base64,type:2}}, function (res) {
                                    if (res.msg == "fail") {
                                        tip(res.info);
                                    }else{
                                        pic_bese64=res.url;
                                       // $('.img img').attr('src',serverurl+res.url)
                                    }
                                })
                            })
//                    var reader = new FileReader;
//                    reader.readAsDataURL(this.files[0]);
//                    reader.onload = function () {
//                        $('.xiaoxi_pic').attr("src",reader.result);
//                        getData({url: uppicurl, data: {userid:uid,base64: reader.result,type:2}}, function (res) {
//                            if (res.msg == "fail") {
//                                tip(res.info);
//                            }else{
//                                pic_bese64=res.url;
//                                $('.img img').attr('src',serverurl+res.url)
//                            }
//                        })
//                    }
                })
 if(type==3){//发送交易请求
     if(state==1){
         $('.bot').hide()
     }else{
         //点击同意
         $('.btn1').off('touchstart').on('touchstart', function () {
             console.log(pic_bese64)
             getData({url:type3,data:{userid:uid,notifyid:notifyid,state:1,thumbnail:pic_bese64}}, function (data) {

                 if(data.msg=="success"){
                     tip(data.info);
                     $('.xiaoxi_detail').hide();
                     $('.all').hide()
                     $('.xiaoxi_pic').attr("src",'');
                     again()
                 }else{
                     tip(data.info)
                 }
             })
         })
         //点击不同意
         $('.btn2').off('touchstart').on('touchstart', function () {
             getData({url:type3,data:{userid:uid,notifyid:notifyid,state:-1}}, function (data) {
                 if(data.msg=="success"){
                     tip(data.info);
                     $('.xiaoxi_detail').hide();
                     $('.all').hide()
                     $('.xiaoxi_pic').attr("src",'');
                     again()
                 }else{
                     tip(data.info)
                 }

             })
         })
     }

   }else if(type==0){//系统消息
        $('.div').hide();
        $('.btn2').hide()
        $('.btn1').html('');
        $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
        $('.btn1').css('marginLeft','40%');
        $('.btn1').css('marginTop','5%')
        if(state==1){
            $('.bot').hide()
        }else{
            $('.btn1').off('touchstart').on('touchstart', function () {
                   getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function () {
                       $('.xiaoxi_detail').hide();
                       $('.all').hide()
                       again()
            })
            })
        }

 }else if(type==1){//别人加你好友
     $('.div').hide();
     $('.bot').css('marginTop','20%')
     if(state==1){
         $('.bot').hide()
     }else{
         $('.btn1').off('touchstart').on('touchstart', function () {
             getData({url:type2,data:{userid:uid,notifyid:notifyid,state:1}}, function (data) {
                 if(data.msg=='success'){
                     tip(data.info);
                     $('.xiaoxi_detail').hide();
                     $('.all').hide()
                     again()
                 }else{
                     tip(data.info)
                 }

             })
         });
         $('.btn2').off('touchstart').on('touchstart', function () {
             getData({url:type2,data:{userid:uid,notifyid:notifyid,state:-1}}, function (data) {
                 if(data.msg=='success'){
                     tip(data.info);
                     $('.xiaoxi_detail').hide();
                     $('.all').hide()
                     again()
                 }else{
                     tip(data.info)
                 }
             })
         })
     }

 }else if(type==2){//好友申请回复
        $('.div').hide();
        $('.btn2').hide();
        $('.btn1').html('');
        $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
        $('.bot').css('marginTop','5%')
        $('.btn1').css('marginLeft','40%');
     if(state==1){
         $('.bot').hide();
     }else{
         $('.btn1').off('touchstart').on('touchstart', function () {
             getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function (data) {
                 if(data.msg=='success'){
                     tip(data.info);
                     $('.xiaoxi_detail').hide();
                     $('.all').hide()
                     again()
                 }else{
                     tip(data.info)
                 }
             })

         })
     }

 }else if(type==4){//交易申请回复
    $('.pic a').hide()
     $('.btn2').hide();
     $('.btn1').html('');
     $('.btn1').before('<input class="xiaoxi_mima" placeholder="请输入支付密码" type="password"/>')
     $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
     $('.btn1').css('marginLeft','40%');
        if(state==1){
            $('.bot').hide();
        }else{
            if(msgtype==1){
                $('.btn1').off('touchstart').on('touchstart', function () {
                    var pwd=MD5($('.xiaoxi_mima').val()+token)
                    getData({url:type4,data:{userid:uid,notifyid:notifyid,state:2,paypassword:pwd}}, function (data) {
                        if(data.msg=='success'){
                            tip(data.info);
                            $('.xiaoxi_detail').hide();
                            $('.all').hide();
                            $(".integral>span").eq(0).text(parseInt(data.sesame));
                            again()
                        }else{
                            tip(data.info)
                        }
                    })
                })
            }else{
                $('.xiaoxi_mima').hide()
                getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function () {
                    $('.xiaoxi_detail').hide();
                    $('.all').hide();
                    again()
                })
            }

        }
 }else if(type==5){
     $('.btn2').hide();
     $('.btn1').html('');
     $('.pic a').hide();
     if(state==1){
         $('.bot').hide();
     }else{

     $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
     $('.btn1').css('marginLeft','40%');
     $('.btn1').off('touchstart').on('touchstart', function () {
         getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function (data) {
             if(data.msg=='success'){
                 tip(data.info);
                 $('.xiaoxi_detail').hide();
                 $('.all').hide();
                 again();
                 getData({url:info,data:{userid:uid}}, function (data) {
                     $(".integral>span").eq(0).text(parseInt(data.sesame));
                 })
             }else{
                 tip(data.info)
             }

         })

     })
     }
 }else if(type==6){

        if(state==1){
            $('.bot').hide()
        }else{
            $('.btn1').off('touchstart').on('touchstart', function () {
                getData({url:type6,data:{userid:uid,notifyid:notifyid,state:2,thumbnail:pic_bese64}}, function (data) {
                    if(data.msg=="fail"){
                        tip(data.info)
                    }else{
                        tip(data.info)
                        $('.xiaoxi_detail').hide();
                        $('.all').hide()
                        again()
                    }
                })
            });
            $('.btn2').off('touchstart').on('touchstart', function () {
                getData({url:type6,data:{userid:uid,notifyid:notifyid,state:-1}}, function (data) {
                    if(data.msg=='success'){
                        tip(data.info);
                        $('.xiaoxi_detail').hide();
                        $('.all').hide()
                        again()
                    }else{
                        tip(data.info)
                    }
                })
            });

        }
 }else if(type==7){
     $('.btn2').hide();
     $('.btn1').html('');
     $('.btn1').before('<input class="xiaoxi_mima" placeholder="请输入支付密码" type="password"/>')
     $('.pic a').hide()
     $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
     $('.btn1').css('marginLeft','40%');
     $('.xiaoxi_pic').attr('src',serverurl+pic);
     if(state==1){
         $('.bot').hide()
     }else{
         if(msgtype==1){
             $('.btn1').off('touchstart').on('touchstart', function () {
                 var pay_pwd=$('.xiaoxi_mima').val();
                 var md5=MD5(pay_pwd+token)   ;
                 getData({url:type7,data:{userid:uid,notifyid:notifyid,state:3,paypassword:md5}}, function (data) {
                     if(data.msg=='success'){
                         tip(data.info);
                         $('.xiaoxi_detail').hide();
                         $('.all').hide()

                         again()
                     }else{
                         tip(data.info)
                     }

                 })
             })
         }else{
             $('.xiaoxi_mima').hide()
             getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function () {
                 $('.xiaoxi_detail').hide();
                 $('.all').hide();
                 again()
             })
         }

     }
 }else if(type==8){
     $('.btn2').hide();
     $('.btn1').html('');
     $('.pic a').hide();
     $('.btn1').css('marginLeft','40%');
     $('.btn1').append('<img src="imgs/queding.png" alt=""/>');
     $('.xiaoxi_pic').attr('src',serverurl+pic);
     $('.xiaoxi_pic').off('touchstart').on('touchstart', function () {

     })
     if(state==1){
         $('.bot').hide()
     }else{
         $('.btn1').off('touchstart').on('touchstart', function () {
             getData({url:info,data:{userid:uid}}, function (data) {
                 $(".integral>span").eq(0).text(parseInt(data.sesame));
                 $('.all').hide()
                 $('.xiaoxi_detail').hide();
                getData({url:typeno,data:{userid:uid,notifyid:notifyid}}, function () {
                    again()
                })
             })
         });
     }
 }

})

        $('.xiaoxi_detail_content').off('touchstart').on('touchstart','.xiaoxi_pic', function () {
                $('.img').show()
                stopTouchendPropagationAfterScroll()
                $('.img img').off('touchend').on('touchend', function () {
                    $('.img').hide()
                })
        })
        //关闭小弹窗
        $('.small_box_close1').off('touchstart').on('touchstart', function () {
           $('.xiaoxi_detail').hide();
           $('.xiaoxi_pic').attr("src",'');
           $('.all').hide();

//            getData({url:typeno,data:{userid:uid,notifyid:xiaoxiID}}, function () {
//                    again()
//            })
        })
        //关闭大弹窗
        $('.close').off('touchstart').on('touchstart', function () {
            $(this).parent().hide();
            $('.xiaoxi_detail').hide();
            $('#box').hide();
        })
    }
    //兑换
    duihuan()
    function duihuan() {
        $('.left li').eq(4).on('touchstart', function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $('#box').show();
            $('.duihuan_box').show().siblings().hide();
            $('.duihuan_box_content ul').html('');
            var type=$('.duihuan_tab').index()+1;
            //请求数据
            getData({url:duihuan_listurl,data:{type:type}}, function (data) {
                  if(data.rows[0]==''){
                      $('.duihuan_box_content').append('<p class="no-data">暂无数据</p>')
                  }else{
                     var html=template('duihuan_list',data);
                      $('.duihuan_box_content ul').html(html)
                  }
            });
            //tab切换请求数据
            $('.duihuan_box_top p').on('touchstart', function () {
                $(this).addClass('duihuan_tab').siblings().removeClass('duihuan_tab');
                type=$(this).index()+1;
                $('.duihuan_box_content ul').html('');
                getData({url:duihuan_listurl,data:{type:type}}, function (data) {
                    if(data.rows[0]==null){
                        $('.duihuan_box_content').append('<p class="no-data">暂无数据</p>')
                    }else{
                        var html=template('duihuan_list',data);
                        $('.duihuan_box_content ul').html(html)
                    }
                });
            })
        });
//        点击图片查看详情
        $('.duihuan_box_content').off('click').on('click','.duihuan_pic', function (e) {
                var desc=$(this).attr('desc')
                $('.duihuan_desc').show();
               $('.duihuan_desc').html(desc)
                $(document).off('touchstart').on('touchstart', function () {
                    $('.duihuan_desc').hide()
                })
        })
        //点击兑换
        $('.duihuan_box_content ul').on('touchstart','.sell', function () {
            //商品id
            $('.duihuan_name').val('');
            $('.duihuan_tel').val('');
            $('.duihuan_dizhi').val('');
            var goodsid=$(this).parents('li').attr('goodsid');
            //兑换商品的名字
            var goods_name=$(this).parents('li').find('.guozi_name').html();
            $('.duihuan_write_info').show();
            $('.last_queding').html('')
            $('.last_queding').html('<img class="duihuan_queding" src="imgs/queding.png" alt=""/>')
            $('.duihuan_goods_name').html(goods_name);
            //确定兑换
            $('.duihuan_queding').on('touchstart', function () {
                var buyer=$('.duihuan_name').val();
                var buyermobile=$('.duihuan_tel').val();
                var address=$('.duihuan_dizhi').val();
                //发送数据
getData({url:duihuanurl,data:{userid:uid,goodsid:goodsid,buyer:buyer,buyermobile:buyermobile,address:address}}, function (data) {
            tip(data.info);
            if(data.msg=='success'){
                $('.duihuan_write_info').hide()
            }
            $('.duihuan_name').val('');
            $('.duihuan_tel').val('');
            $('.duihuan_dizhi').val('');
//            if(data.msg==success){
//                $('.duihuan_write_info').hide();
//            }
    })
  })
})
        //关闭小弹窗
        $('.small_box_close2').off('touchstart').on('touchstart', function () {
            $(this).parents('.duihuan_write_info').hide();
        })
        //关闭
        $('.close').on('touchstart', function () {
            $(this).parent().hide();
            $('#box').hide();
            $('.duihuan_write_info').hide()
        })
    }
    //管家
    guanjia();
    function guanjia() {
        $('.guanjia').on('touchstart', function () {
            if(certificationstate!=2){
                tip('请先实名认证');
                return
            }
            $('#box').show();
            $('.guajia_box').show().siblings().hide();

            if(butler!=1){
                $('.guajia_box p').html('聘用管家需要消耗1000芝麻，<br/>确定聘用？')
            }else{
                $('.guajia_box p').html('续费管家需要消耗1000芝麻(50天)，<br/>确定续费？')
            }

            //点击确定
            $(' .guajia_box .determine').off('touchstart').on('touchstart', function () {
                  getData({url:guanjiaurl,data:{userid:uid}}, function (data) {
                      if(data.msg=='success'){
                            tip(data.info);
                            $('.guajia_box').hide();
                            setTimeout(function () {
                                $('#box').hide()
                            },400)
                            var gtime=data.butlersurplustime;
                            var newtime=gtime.split(' ')[0]
                            $('.time').text('剩余'+newtime)
                            $(".integral>span").eq(0).text(parseInt(data.sesame));
                      }else{
                          tip(data.info)
                      }
                  })
            })
            //点击取消
            $('.guajia_box .cancel').off('touchstart').on('touchstart', function () {
                $('.guajia_box').hide();
                $('.guajia_box').hide();
                setTimeout(function () {
                    $('#box').hide()
                },400)
            })
        })
    }
    //设置
    setting();
    function setting() {
        $('.top_right li').eq(0).on('touchstart', function () {
            $('#box').show();
            $('.set_box').show();
        })
        //点击开关
        var au=document.getElementById('audio')
        $('.set_box_content .off_on').off('touchstart').on('touchstart', function () {
                $(this).toggleClass('loop');
            stopBgm()
//            if(au.paused){
//                au.play();
//            }else{
//                au.pause();
//            }
        });
        //点击注销
        $('.tuichu').off('touchstart').on('touchstart', function () {
                getData({url:exit,data:{userid:uid}}, function (data) {
                        if(data.msg=='success'){
                            tip(data.info)
                            window.location.href='login.html'
                        }else{
                            tip(data.info)
                        }
                })

        })
        $('.set_box .close').off('touchstart').on('touchstart', function () {
            $('.set_box').hide();
            $('#box').hide();
        })
    }
    //修改登录密码
    updata_login_pwd();
    function updata_login_pwd() {
        //点击修改
        $('.xiugai_login_pwd').off('touchstart').on('touchstart', function () {
            $('.member_info').hide();
            $('.choose_login_pwd_box').show();
            $('.choose_login_pwd_box input').val('')
        })
        //点击确定
        $('.getyzm').off('touchstart').on('touchstart', function () {
               getData({url:getyzm,data:{userid:uid,type:2}}, function (data) {
                       tip(data.info)
               })
        })
        $('.pwd_bot').off('touchstart').on('touchstart', function () {
                var newpwd=$('.new_pwd').val();
                var again_pwd=$('.again_pwd').val();
                var yzm=$('.pwd_yzm').val();
                if(newpwd==''){
                    tip('新密码不能为空');
                     return
                }
                if(newpwd!=again_pwd){
                    tip('两次输入的密码不一致');
                    return
                }
                if(yzm==''){
                    tip('验证码不能为空');
                    return
                }
                getData({url:xiugaiurl,data:{userid:uid,loginpwdcode:yzm,loginpassword:newpwd}}, function (data) {
                        if(data.msg=='success'){
                            tip(data.info);
                            $('.choose_login_pwd_box').hide();
                            $('#box').hide();
                        }else{
                            tip(data.info)
                        }
                })
        })
        //点击关闭
        $('.choose_login_pwd_box .small_box_close').off('touchstart').on('touchstart', function () {
            $('.choose_login_pwd_box').hide();
            $('#box').hide();
        })
    }
    //修改支付密码
    updata_pay_pwd();
    function updata_pay_pwd() {
        //点击修改
        $('.xiugai_pay_pwd').off('touchstart').on('touchstart', function () {
            $('.member_info').hide();
            $('.choose_login_pwd_box1').show();
            $('.choose_login_pwd_box1 input').val('')
        })
        $('.getyzm1').off('touchstart').on('touchstart', function () {
            getData({url:getyzm,data:{userid:uid,type:3}}, function (data) {
                tip(data.info)
            })
        })
        $('.pwd_bot1').off('touchstart').on('touchstart', function () {
            var newpwd=$('.new_pwd1').val();
            var again_pwd=$('.again_pwd1').val();
            var yzm=$('.pwd_yzm1').val();
            if(newpwd==''){
                tip('新密码不能为空');
                return
            }
            if(newpwd!=again_pwd){
                tip('两次输入的密码不一致');
                return
            }
            if(yzm==''){
                tip('验证码不能为空');
                return
            }
            getData({url:xiugaiurl,data:{userid:uid,paypwdcode:yzm,paypassword:newpwd}}, function (data) {
                if(data.msg=='success'){
                    tip(data.info);
                    $('.choose_login_pwd_box1').hide();
                    $('#box').hide();
                }else{
                    tip(data.info)
                }
            })
        })
        //点击关闭
        $('.choose_login_pwd_box1 .small_box_close').off('touchstart').on('touchstart', function () {
            $('.choose_login_pwd_box1').hide();
            $('#box').hide();
        })
    }
    //修改资料
    updata_wx();
    function updata_wx() {
           $('.change_info').off('touchstart').on('touchstart', function () {
               if(certificationstate!=2){
                   tip('请先实名认证');
                   return
               }
               $('.member_info').hide();
               $('.choose_login_pwd_box2').show();
               $('.choose_login_pwd_box2 input').val('')
               $('.pwd_bot2').off('touchstart').on('touchstart', function () {
                   var wxid=$('.choose_login_pwd_box2 input').eq(0).val();
                   var alid=$('.choose_login_pwd_box2 input').eq(1).val();
                   var bank=$('.choose_login_pwd_box2 input').eq(2).val();
                   var bank_at=$('.choose_login_pwd_box2 input').eq(3).val();
                   if(wxid==''){
                       tip('微信号不能为空');
                       return
                   }
                   if(alid==''){
                       tip('支付宝不能为空');
                       return
                   }
                   if(bank==''){
                       tip('银行卡不能为空');
                       return
                   }
                   if(bank_at==''){
                       tip('开户支行不能为空');
                       return
                   }
                   getData({url:xiugaiurl,data:{userid:uid,wxid:wxid,alipayno:alid,cardno:bank,bank:bank_at}}, function (data) {
                       if(data.msg=='success'){
                           tip(data.info);
                           $('.choose_login_pwd_box2').hide();
                           $('#box').hide();
                       }else{
                           tip(data.info)
                       }
                   })
               })
           });
            $('.choose_login_pwd_box2 .small_box_close').off('touchstart').on('touchstart', function () {
            $('.choose_login_pwd_box2').hide();
            $('#box').hide();
        })
    }
////    修改支付宝
//    updata_ali();
//    function updata_ali() {
//        $('.xiugai_ali_pwd').off('touchstart').on('touchstart', function () {
//            $('.member_info').hide();
//            $('.choose_login_pwd_box3').show();
//            $('.choose_login_pwd_box3 input').val('')
//            $('.pwd_bot3').off('touchstart').on('touchstart', function () {
//                var alid=$('.choose_login_pwd_box3 input').val();
//                getData({url:xiugaiurl,data:{userid:uid,alipayno:alid}}, function (data) {
//                    if(data.msg=='success'){
//                        tip(data.info);
//                        $('.choose_login_pwd_box3').hide();
//                        $('#box').hide();
//                    }else{
//                        tip(data.info)
//                    }
//                })
//            })
//        })
//        $('.choose_login_pwd_box3 .small_box_close').off('touchstart').on('touchstart', function () {
//            $('.choose_login_pwd_box3').hide();
//            $('#box').hide();
//        })
//    }
//    //修改银行卡号
//    updata_bank_card();
//    function updata_bank_card() {
//        $('.add_bank_card').off('touchstart').on('touchstart', function () {
//            $('.member_info').hide();
//            $('.choose_login_pwd_box4').show();
//            $('.choose_login_pwd_box4 input').val('')
//            $('.pwd_bot4').off('touchstart').on('touchstart', function () {
//                var alid=$('.choose_login_pwd_box4 input').val();
//                getData({url:xiugaiurl,data:{userid:uid,cardno:alid}}, function (data) {
//                    if(data.msg=='success'){
//                        tip(data.info);
//                        $('.choose_login_pwd_box4').hide();
//                        $('#box').hide();
//                    }else{
//                        tip(data.info)
//                    }
//                })
//            })
//        })
//        $('.choose_login_pwd_box4 .small_box_close').off('touchstart').on('touchstart', function () {
//            $('.choose_login_pwd_box4').hide();
//            $('#box').hide();
//        })
//    }
//    //开户支行
//    updata_bank_at();
//    function updata_bank_at() {
//        $('.add_bank_at').off('touchstart').on('touchstart', function () {
//            $('.member_info').hide();
//            $('.choose_login_pwd_box5').show();
//            $('.choose_login_pwd_box5 input').val('')
//            $('.pwd_bot5').off('touchstart').on('touchstart', function () {
//                var alid=$('.choose_login_pwd_box5 input').val();
//                getData({url:xiugaiurl,data:{userid:uid,bank:alid}}, function (data) {
//                    if(data.msg=='success'){
//                        tip(data.info);
//                        $('.choose_login_pwd_box4').hide();
//                        $('#box').hide();
//                    }else{
//                        tip(data.info)
//                    }
//                })
//            })
//        })
//        $('.choose_login_pwd_box5 .small_box_close').off('touchstart').on('touchstart', function () {
//            $('.choose_login_pwd_box5').hide();
//            $('#box').hide();
//        })
//    }
    //实名认证
    sm();
    function sm() {
        $('.shimingbtn').on('touchstart', function () {
            $('.shiming').show();
            //初始化图片
            $('.zm').attr('src','imgs/zhengmian.png');
            $('.fm').attr('src','imgs/fanmian.png');
            $('.shiming_box_content input').val('');
        })
        var firstpic;
        var lastpic;
        $('.sm1').val('');
        $('.sm1').on('change', function () {
            var reader = new FileReader;
            reader.readAsDataURL(this.files[0]);
            reader.onload = function () {
                $('.zm').attr("src",reader.result);
                getData({url: uppicurl, data: {userid:uid,base64: reader.result,type:2}}, function (res) {
                    if (res.msg == "success") {
                        tip(res.info);
                        firstpic=res.url;
                    }else{
                        tip(res.info);
                    }
                })
            }
        })
        $('.sm2').val('');
        $('.sm2').on('change', function () {
            var reader = new FileReader;
            reader.readAsDataURL(this.files[0]);
            reader.onload = function () {
                $('.fm').attr("src",reader.result);
                getData({url: uppicurl, data: {userid:uid,base64: reader.result,type:2}}, function (res) {
                    if (res.msg == "fail") {
                        tip(res.info);
                    }else{
                        lastpic=res.url;
                    }
                })
            }
        })
        //点击提交
        $('.bottom').off('touchstart').on('touchstart', function () {
            var name=$('.sm_name').val();
            var sfcard=$('.sm_card').val();//idcardface:firstpic,idcardback:lastpic
            var wx=$('.wx').val();
            var ali=$('.ali').val();
            var bank=$('.bank_card').val();
            var bank_at=$('.bank_at').val();
            getData({url:smurl,data:{userid:uid,name:name,paparno:sfcard,wxid:wx,alipayno:ali,cardno:bank,bank:bank_at}}, function (data) {
                if(data.msg=='success'){
                    tip(data.info);
                    $('.shiming').hide();
                }else{
                    tip(data.info)
                }
            })
        })
        //关闭
        $('.shiming .small_box_close3').on('touchstart', function () {
            $('.shiming').hide();
        })
    }
});
